package com.baibei.ebec.guide;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.crossboarder.R;
import com.baibei.ebec.sdk.BuildConfig;
import com.baibei.module.AppRouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AdvertisementActivity extends AppCompatActivity {

    @BindView(R.id.iv_ad)
    ImageView ivAd;
    @BindView(R.id.tv_count)
    TextView tvCount;

    private int recLen = 5;
    private int time;
    private boolean isFisrtStart;
    private SharedPreferences sp;
    //    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertisement);
        ButterKnife.bind(this);
        sp = getSharedPreferences("firstStart", MODE_PRIVATE);
        isFisrtStart = sp.getBoolean("isFirstStart", true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadAdImg();
            }
        }).start();

    }

    private void loadAdImg() {
        String adurl = BuildConfig.API_BASE_URL + "HD";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(adurl)
                .build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            JSONObject json = new JSONObject(response.body().string());
            Log.i("hhh", json.toString());
            String code = json.optString("code");
            if (!"SUCCESS".equals(code)) {
                handler.postDelayed(runnable, 1000);
                return;
            }
            JSONObject data = json.optJSONObject("data");
            final String image = data.optString("image");
            time = (int) data.optDouble("time");
//            url = data.optString("url");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Glide.with(getApplicationContext()).load(image).into(ivAd);
                    recLen = time;
                    handler.postDelayed(runnable, 1000);
                }
            });
        } catch (Exception e) {
            handler.postDelayed(runnable, 1000);
            e.printStackTrace();
        }
    }

    @OnClick({R.id.iv_ad, R.id.tv_count})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_ad:
//                toAdvertWeb();
                break;
            case R.id.tv_count:
                toMain();

                break;
        }
    }

    Handler handler = new Handler();

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            recLen--;
            if (recLen != 0) {
                tvCount.setText("点击跳过 " + recLen);
                handler.postDelayed(this, 1000);
            } else {
                toMain();

            }
        }
    };

    private void toMain() {
        if (isFisrtStart) {
            AppRouter.routeToNewhand(AdvertisementActivity.this);
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("isFirstStart", false);
            editor.commit();
            handler.removeCallbacks(runnable);
            finish();
        } else {
            AppRouter.routeToMain(this);
            handler.removeCallbacks(runnable);
            finish();
        }
    }

//
//    private void toAdvertWeb() {
//        AppRouter.routeToWeb(this,url);
//    }
}
